﻿using System;
using System.IO;
using CommDll;
using System.Collections.Generic;
using System.Windows.Forms;


namespace InstitutionAssitant
{
    /// <summary>
    /// 系统参数类
    /// </summary>
    public class AppConfig
    {
        //配置文件
        public static IniFile m_iniSetup = new IniFile(Path.GetFullPath("Config.ini"));
        // 01 系统设置类
        public static string m_strUserBefore = "";  //登录用户
        public static int m_nLoginFailedTimes = 5;  // 登录失败次数        
        // 02 系统使用类
        // 是否包含订单信息
        public static Boolean m_bIsHaveOrder = false;
        //消息提示是否正常
        public static Boolean m_bIsMessageOK = true;
        
        // 每页记录数
        public static int m_nPageSize = 100000;        
        // 日期提前量
        public static int m_nDateBeforeOrder = 0;   // 订单
        public static int m_nDateBeforeDistribution = 0;    // 配送
        public static int m_nDateBeforeInvoice = 0; //发票
        // DataViewGrid显示信息
        public static Dictionary<string, string> m_dictFieldSql = new Dictionary<string, string>(); // Sql字段值
        public static Dictionary<string, string> m_dictFieldName = new Dictionary<string, string>();    // 字段值
        public static Dictionary<string, string> m_dictField0 = new Dictionary<string, string>();   // 显示字段默认值
        public static Dictionary<string, string> m_dictField = new Dictionary<string, string>();    // 显示字段设置值        
        public static Dictionary<string, string> m_dictFieldWidth0 = new Dictionary<string, string>();  // 显示字段宽度默认值
        public static Dictionary<string, string> m_dictFieldWidth = new Dictionary<string, string>();   // 显示字段宽度设置值
        //是否价格限制(不符可以入库)
        public static Boolean m_bIsPriceLimit = true;
        /// <summary>
        /// 读取配置数据
        /// </summary>
        public static void _readData()
        {
            // 01 基础信息            
            m_strUserBefore = m_iniSetup._readValue("Common", "User", "").Trim();
            m_bIsMessageOK = FunctionComm._strToInt(m_iniSetup._readValue("Common", "IsMessageOK", "1").Trim(), 0) == 1;
            m_bIsHaveOrder = FunctionComm._strToInt(m_iniSetup._readValue("Common", "IsHaveOrder", "1").Trim()) == 1;
            m_bIsHaveOrder = true;
            m_nDateBeforeOrder = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeOrder", "3").Trim(), 0);
            m_nDateBeforeDistribution = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeDistribution", "3").Trim(), 0);
            m_nDateBeforeInvoice = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeInvoice", "3").Trim(), 0);
        }

        /// <summary>
        /// 保存DataGridView字段显示信息
        /// </summary>
        public static void _saveDgvTitle(string strKind)
        {
            m_iniSetup._writeValue("Common", string.Format("{0}_Field", strKind), m_dictField[strKind]);
            m_iniSetup._writeValue("Common", string.Format("{0}_FieldWidth", strKind), m_dictFieldWidth[strKind]);
        }

        /// <summary>
        /// 保存dgv宽度数据
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="strKind"></param>
        /// <param name="bIsSelect">有选择框时特殊处理</param>
        public static void _saveDgvWidth(DataGridView dgv,string strKind,Boolean bIsSelect = false)
        {
            string strWidth = "";
            int nBegin = 0;
            nBegin = bIsSelect ? 2 : 0;
            for (int i = nBegin; i < dgv.ColumnCount; i++)
            {
                strWidth += (strWidth.Equals("")?"":",") + (dgv.Columns[i].Visible ? dgv.Columns[i].Width.ToString():"0");
            }
            // 保存数据
            m_dictFieldWidth[strKind] = strWidth;
            m_iniSetup._writeValue("Common", string.Format("{0}_FieldWidth", strKind), m_dictFieldWidth[strKind]);            
        }
    }
}